export function parseTime(time, pattern) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    } else if (typeof time === 'string') {
      time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '')
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}


export function fixZeroStart(str, n) {
    return (Array(n).join(0) + str).slice(-n);
}

export function formatSeconds(value) {
	let secondTime = parseInt(value);// 秒
	let minuteTime = 0;// 分
	let hourTime = 0;// 小时
	if(secondTime > 60) {//如果秒数大于60，将秒数转换成整数
			//获取分钟，除以60取整数，得到整数分钟
			minuteTime = parseInt(secondTime / 60);
			//获取秒数，秒数取余，得到整数秒数
			secondTime = parseInt(secondTime % 60);
			//如果分钟大于60，将分钟转换成小时
			if(minuteTime > 60) {
					//获取小时，获取分钟除以60，得到整数小时
					hourTime = parseInt(minuteTime / 60);
					//获取小时后取余的分，获取分钟除以60取余的分
					minuteTime = parseInt(minuteTime % 60);
			}
	}
	let result = "" + parseInt(secondTime) + "秒";

	if(minuteTime > 0) {
		result = "" + parseInt(minuteTime) + "分" + result;
	}
	if(hourTime > 0) {
		result = "" + parseInt(hourTime) + "小时" + result;
	}
	return result;
}

export function numberToChinese(num) {
  if (typeof num !== 'number' || isNaN(num) || num < 0) {
    return '输入必须是非负整数';
  }

  // 定义中文数字和单位
  const chineseNumbers = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
  const chineseUnits = ['', '十', '百', '千', '万', '亿'];

  // 处理个位数
  if (num < 10) {
    return chineseNumbers[num];
  }

  // 处理两位数及以上
  let strNum = num.toString();
  let len = strNum.length;
  let chineseStr = '';

  for (let i = 0; i < len; i++) {
    const n = parseInt(strNum[i]);
    const digit = chineseNumbers[n];
    const unit = len - 1 - i > 0 ? chineseUnits[len - 1 - i] : '';
    if (n !== 0) {
      chineseStr += digit + unit;
    }
  }

  // 处理连续零的情况
  chineseStr = chineseStr.replace(/零(千|百|十)/g, '零$1').replace(/零+/g, '零').replace(/零(千|百|十|一)/g, '$1').replace(/零$/g, '');

  return chineseStr;
}